//
//  SelectBtns.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/4.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "SelectBtns.h"
@interface SelectBtns ()

@property (nonatomic, strong)    UIButton *leftBtn;
@property (nonatomic, strong)     UIButton *rightBtn;
@property (nonatomic, strong)     UIView *lineView;

@end
@implementation SelectBtns
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        selfFrame = frame;
        [self addSubview:self.leftBtn];
        [self addSubview:self.lineView];
        [self addSubview:self.rightBtn];
    }
    return  self;
}

- (UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.frame = CGRectMake(0, 0, (selfFrame.size.width-1)/2, selfFrame.size.height);
        [_leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _leftBtn.titleLabel.font    = [UIFont systemFontOfSize: kHeight(60)];
        [_leftBtn addTarget:self action:@selector(handleLeft:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}
- (UIView *)lineView{
    if (!_lineView ) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.leftBtn.frame), 0, 1, selfFrame.size.height)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#6e708c"];
    }
    return _lineView;
}
- (UIButton *)rightBtn{
    if (!_rightBtn ) {
        _rightBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(CGRectGetMaxX(self.lineView.frame), 0, (selfFrame.size.width-1)/2, selfFrame.size.height);
        [_rightBtn setTitleColor:[UIColor colorWithHexString:@"#6e708c"] forState:UIControlStateNormal];
        _rightBtn.titleLabel.font    = [UIFont systemFontOfSize: kHeight(60)];
        [_rightBtn addTarget:self action:@selector(handleRight:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}
- (void)setBtntitleWithArray:(NSArray *)titleArray{
    if (titleArray.count ==2) {
        CGSize leftSize  = [self labelAutoCalculateRectWith:[NSString stringWithFormat:@"%@", titleArray.firstObject] FontSize:kHeight(60) MaxSize:CGSizeMake(selfFrame.size.width, selfFrame.size.height)];
        _leftBtn.frame = CGRectMake(0, 0, leftSize.width, selfFrame.size.height);
        _lineView.frame = CGRectMake(CGRectGetMaxX(self.leftBtn.frame) + kWidth(25), 0, 1, selfFrame.size.height);
        CGSize rightSize  = [self labelAutoCalculateRectWith:[NSString stringWithFormat:@"%@", titleArray.lastObject] FontSize:kHeight(60) MaxSize:CGSizeMake(selfFrame.size.width, selfFrame.size.height)];
        _rightBtn.frame = CGRectMake(CGRectGetMaxX(self.lineView.frame) + kWidth(25), 0, rightSize.width, selfFrame.size.height);
        [self.leftBtn setTitle:[NSString stringWithFormat:@"%@", titleArray.firstObject] forState:UIControlStateNormal];
        [self.rightBtn setTitle:[NSString stringWithFormat:@"%@", titleArray.lastObject] forState:UIControlStateNormal];
    }}
-(void)handleRight:(UIButton *)sender{
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_leftBtn setTitleColor:[UIColor colorWithHexString:@"#6e708c"] forState:UIControlStateNormal];
    if ([self.delegate respondsToSelector:@selector(selectRightBtn)]) {
        [self.delegate selectRightBtn];
    }
}
-(void)handleLeft:(UIButton *)sender{
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_rightBtn setTitleColor:[UIColor colorWithHexString:@"#6e708c"] forState:UIControlStateNormal];
    if ([self.delegate respondsToSelector:@selector(selectLeftBtn)]) {
        [self.delegate selectLeftBtn];
    }
}
- (CGSize)labelAutoCalculateRectWith:(NSString *)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize

{
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary * attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    
    
    CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    
    
    
    labelSize.height = ceil(labelSize.height);
    
    labelSize.width = ceil(labelSize.width);
    
    return labelSize;
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
